package com.dx.interceptor;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.dx.pojo.Account;

public class LoginInterceptor implements HandlerInterceptor{

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		System.out.println("你好阿");
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		Account account=(Account) session.getAttribute("admin");
		Account user=(Account)session.getAttribute("account");
		if(user!=null){
			return true;
		}else{
			 response.setContentType("text/html; charset=utf-8");
	    	 PrintWriter pw=response.getWriter();
	    	 pw.print("<script>alert('对不起，请你先登录')</script>");
	    	 pw.print("<script>location.href='http://localhost:8080/graduation/user/main.jsp'</script>");
	    	 pw.close();
			
			return false;
		}
		
		//如果用户身份在session中存在放行
		
		
		
	}

}
